# 子模块的目录路径
$submoduleDir = "src/main/HealthSystem/backend"
$getback = "../../../.."
# 检查子模块目录是否存在
if (-not (Test-Path $submoduleDir)) {
    # 子模块不存在，添加子模块
    $remoteRepoUrl = "https://gitee.com/Hykerisme/health-system-server.git"
    git submodule add $remoteRepoUrl $submoduleDir
}

cd $submoduleDir
git checkout master
git reset --hard HEAD
git clean -fd
git pull origin

# 更新子模块指针
git submodule update --remote --merge
cd $getback

Write-Host "update finished"
Write-Host "adding Package Declaration"

# 检查子模块目录是否存在
if (Test-Path $submoduleDir) {
    # 获取所有Java文件
    $javaFiles = Get-ChildItem -Path $submoduleDir -Recurse -Filter *.java

    foreach ($file in $javaFiles) {
        # 读取文件内容，指定UTF-8编码
        $content = Get-Content -Path $file.FullName -Encoding UTF8 -Raw

        # 检查文件是否已经包含package声明
        if ($content -notmatch '^package backend;') {
           # 在文件开头添加package声明
           $newContent = "package backend;" + [Environment]::NewLine +$content
           # 写回文件，指定UTF-8编码
           Set-Content -Path $file.FullName -Value $newContent -Encoding UTF8
           Write-Host "Added package declaration to $($file.FullName)"
        } else {
            Write-Host "Package declaration already exists in $($file.FullName), do nothing."
        }
    }
} else {
    Write-Host "The submodule directory does not exist: $submoduleDir"
}
